package com.yangjun.common.excel;

import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.google.common.collect.Maps;
import com.yangjun.dao.mybatis.mapper.report.DataBaseExcelDefinitionMapper;
import com.yangjun.entity.report.DvExcelDefinitionEntity;
import com.yangjun.entity.report.DvExcelFieldDefinitionEntity;
import com.yangjun.utils.ReflectUtil;

@Component
public class DataBaseExcelDefinitionReader implements ExcelDefinitionReader{
	@Autowired
	private DataBaseExcelDefinitionMapper dataBaseExcelDefinitionMapper;
	private List<DvExcelDefinitionEntity> list;
	private Map<String,ExcelDefinition> registry;
	
	public Map<String,String> init(String type){
		Map<String,String> resultMap = Maps.newHashMap();
		list = dataBaseExcelDefinitionMapper.query(type);
		if(list.size()>0){
			registry = Maps.newHashMap();
			for(DvExcelDefinitionEntity definitionEntity : list){
				processExcelDefinition(definitionEntity);
				resultMap.put(definitionEntity.getExcelId(), definitionEntity.getExcelName());
			}
		}
		return resultMap;
	}
	private void processExcelDefinition(DvExcelDefinitionEntity definitionEntity){
		ExcelDefinition excelDefinition = new ExcelDefinition();
		String id = definitionEntity.getExcelId();
		if(StringUtils.isBlank(id)){
			throw new ExcelException("Excel 配置文件 id为 [ null ] ");
		}
//		if(registry.containsKey(id)){
//			throw new ExcelException("Excel 配置文件 id为 [ "+id+" ] 的模板不止一个");
//		}
		excelDefinition.setId(id);
		
		String className = definitionEntity.getClassName();
		if(StringUtils.isBlank(className)){
			throw new ExcelException("Excel 配置文件 id为 [ "+id+" ] 的 class 为 [ null ]");
		}
		
		Class<?> clazz = null;
		try {
			clazz = Class.forName(className);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		excelDefinition.setClassName(className);
		excelDefinition.setClazz(clazz);
		String defaultColumnWidth = definitionEntity.getDefaultCoulmnWidth();
		if(StringUtils.isNotBlank(defaultColumnWidth)){
			try{
				excelDefinition.setDefaultColumnWidth(Integer.valueOf(defaultColumnWidth));
			}catch(NumberFormatException e){
				throw new ExcelException("Excel 配置文件 id为 [ " + id
				+ " ] 的 defaultColumnWidth 属性只能为int类型");
			}
		}
		
		//设置sheetName
		String sheetName = definitionEntity.getSheetName();
		if(StringUtils.isNotBlank(sheetName)){
			excelDefinition.setSheetname(sheetName);
		}
		//设置对齐方式
		String defaultAlign= definitionEntity.getDefaultAlign();
		if(StringUtils.isNotBlank(defaultAlign)){
			try{
			excelDefinition.setDefaultAlign(HorizontalAlignment.CENTER);
			}catch(Exception e){
				throw new ExcelException("Excel 配置文件 id为 [ " + id
				+ " ] 的 defaultAlign 属性不能为 [ "+defaultAlign+" ],目前支持的left,center,right");
			}
		}
		//设置sheet index
		String sheetIndex = definitionEntity.getSheetIndex();
		if(StringUtils.isNotBlank(sheetIndex)){
			try{
				int index = Integer.parseInt(sheetIndex);
				if(index<0){
					throw new ExcelException("Excel 配置文件id为 [ " + id
					+ " ] 的 sheetIndex 属性不能为 [ "+index+" ],索引位置至少从0开始");
				}
				excelDefinition.setSheetIndex(index);
			}catch(NumberFormatException e){
				throw new ExcelException("Excel 配置文件id为 [ " +id
				+ " ] 的 sheetIndex 属性不能为 [ "+sheetIndex +" ],只能为int类型");
			}

		}
		processField(id,excelDefinition);
		registry.put(id, excelDefinition);
	}
	protected void processField(String excelId,ExcelDefinition excelDefinition){
		List<DvExcelFieldDefinitionEntity> dvExcelFieldDefinitionList =  dataBaseExcelDefinitionMapper.queryField(excelId,null);
		if(dvExcelFieldDefinitionList.size()>0){
			for(DvExcelFieldDefinitionEntity fieldEntity : dvExcelFieldDefinitionList){
				FieldValue fieldValue = new FieldValue();

				String entityName = fieldEntity.getEntityName();
				if(StringUtils.isBlank(entityName)){
					throw new ExcelException("Excel 配置文件id为 [ "+excelDefinition.getId()+" ] 的 name 属性不能为 [ null ]");
				}
				fieldValue.setName(entityName);

				String title = fieldEntity.getTitle();
				if(StringUtils.isBlank(title)){
					throw new ExcelException("Excel 配置文件id为 [ "+excelDefinition.getId()+" ] 的 title 属性不能为 [ null ]");
				}
				fieldValue.setTitle(title);
				
				String pattern = fieldEntity.getPattern();
				if(StringUtils.isNotBlank(pattern)){
					fieldValue.setPattern(pattern);
				}
				
				excelDefinition.getFieldValues().add(fieldValue);
			}
		}
		
	}
	public  DataBaseExcelDefinitionReader() {
		registry = Maps.newHashMap();
		
	}
	
	/**
	 * @return 不可被修改的注册信息
	 */
	@Override
	public Map<String, ExcelDefinition> getRegistry() {
        // 不能put remove 操作但可以对里的对象进行操作
		return Collections.unmodifiableMap(registry);
	}
}
